package com.aries.crossgate.modules.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.aries.crossgate.common.log.config.Log;
import com.aries.crossgate.common.utils.Constant;
import com.aries.crossgate.common.utils.JsonDataFormate;
import com.aries.crossgate.common.utils.R;
import com.aries.crossgate.modules.blog.entity.BlogArticle;
import com.aries.crossgate.modules.blog.service.BlogArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("blog/article")
public class BlogArticleController {

    @Autowired
    private BlogArticleService blogArticleService;

    @RequestMapping("index.html")
    public String index() {
        return "blog/admin/blogList";
    }


    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/addPage.html")
    public String addPage() {
        return "blog/admin/blogArticle";
    }


    /**
     *  跳转到保存页面
     * @return
     */
    @RequestMapping(value = "/savePage.html")
    public String add(){
        return "blog/admin/blogArticleSave";
    }


    @Log("文章列表")
    @ResponseBody
    @RequestMapping(value = "/getArticle.html")
    public JSONObject getArticle(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<BlogArticle> list= blogArticleService.selectList(null);
        PageInfo pageInfo = new PageInfo(list);
        JSONObject jsonObject = JsonDataFormate.piecing(list,pageInfo);
        return jsonObject;
    }

    @Log("保存用户")
    @RequestMapping(value = "/save.html")
    @ResponseBody
    public R save(BlogArticle blogArticle){
        //sysUser.setUserId(4L);
        if(blogArticleService.insert(blogArticle)){
            return R.ok(Constant.SYSUSER);
        }else {
            return R.error(Constant.ERROR);
        }
    }
}